package office.service.hr.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import office.action.hr.form.OfficesuppliesEntryForm;
import office.bo.hr.IOfficesuppliesEntry;
import office.bo.hr.impl.Officesupplies;
import office.bo.hr.impl.OfficesuppliesEntry;
import office.domainset.BizDomainProviderSetImpl;
import office.service.hr.IOfficesuppliesEntryService;
import platform.common.exception.BusinessException;
import platform.common.service.impl.BaseBizServiceImpl;

public class OfficesuppliesEntryServiceImpl extends BaseBizServiceImpl implements IOfficesuppliesEntryService{

	/**
	 * 获取申请办公用品信息
	 * @return
	 * @throws BusinessException
	 */
	public List getOfficesuppliesEntryList(String userId) throws BusinessException{
		List listData = new ArrayList() ;
		try {
			//Map mapCarriageType = Util.parsePropertiesReturnMap("office","carriageType.size");
			//Map mapCarriageSate = Util.parsePropertiesReturnMap("office","carriageSate.size");
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			Map map = new HashMap();
			map.put("userId", userId);
			listData = domainProviderSetImpl.getDao().findByHqlNameAndDomian(domainProviderSetImpl.getOfficesuppliesEntryDP(), "findAll", map);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("获取申请办公用品信息失败!", e);
		}
		return listData;
	}
	/**
	 * 编辑申请办公用品
	 * @throws BusinessException
	 */
	public void editOfficesuppliesEntry(OfficesuppliesEntryForm objctForm)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			OfficesuppliesEntry bo = null;
			if(null==objctForm.getId() || ("").equals(objctForm.getId())){
				bo = (OfficesuppliesEntry)domainProviderSetImpl.getOfficesuppliesEntryDP().newEmptyObj();
			}else{
				bo = (OfficesuppliesEntry)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getOfficesuppliesEntryDP(), objctForm.getId());
			}
			bo =(OfficesuppliesEntry) objctForm.copyPropertiesToBO(bo);// 拷贝了申请数量、描述
			if(null==bo.getId() || ("").equals(bo.getId())){
				bo.generatorUUID();
				bo.setEntryUser(objctForm.getEntryUser());
				bo.setOfficesuppliesId(objctForm.getOfficesuppliesId());
				bo.setState("0");
				Calendar c = Calendar.getInstance();
				SimpleDateFormat dateFmt = new SimpleDateFormat("yyyy-MM-dd");
				bo.setEntryDate(dateFmt.format(c.getTime()));
			}
			//申请通过需要把办公用品的库存量扣除
			if(null!=bo.getOfficesuppliesId() && !("").equals(bo.getOfficesuppliesId())){
				String officesuppliesId = bo.getOfficesuppliesId();
				Officesupplies officesupplies = (Officesupplies)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getOfficesuppliesDP(), officesuppliesId);
				if(officesupplies.getRepertory()<bo.getEntryNum()){
					throw new BusinessException("申请数量不能操作库存数量!");
				}
			}
			//验证是否存在重复记录
			bo.store(domainProviderSet);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("编辑申请办公用品信息失败!", e);
		}
	}
	/**
	 * 根据id获取对象
	 * @param id
	 * @return
	 * @throws BusinessException
	 */
	public OfficesuppliesEntryForm getById(String id)throws BusinessException{
		OfficesuppliesEntryForm form = new OfficesuppliesEntryForm();
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			IOfficesuppliesEntry bo = (IOfficesuppliesEntry)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getOfficesuppliesEntryDP(), id);
			form.copyPropertiesFromBO(bo);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据id获取申请办公用品信息对象失败!", e);
		}
		return form;
	}
	/**
	 * 根据id删除对象
	 * @param id
	 * @throws BusinessException
	 */
	public void deleteById(String id)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			domainProviderSetImpl.getDao().deleteById(domainProviderSetImpl.getOfficesuppliesEntryDP(), id);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据id删除申请办公用品信息对象失败!", e);
		}
	}
	/**
	 * 修改状态
	 * @param objctForm
	 * @throws BusinessException
	 */
	public void changeState(OfficesuppliesEntryForm objctForm)throws BusinessException{
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			OfficesuppliesEntry bo = (OfficesuppliesEntry)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getOfficesuppliesEntryDP(), objctForm.getId());
			
			//申请通过需要把办公用品的库存量扣除
			if(null!=bo.getOfficesuppliesId() && !("").equals(bo.getOfficesuppliesId())){
				String officesuppliesId = bo.getOfficesuppliesId();
				Officesupplies officesupplies = (Officesupplies)domainProviderSetImpl.getDao().findByIdAndDomain(domainProviderSetImpl.getOfficesuppliesDP(), officesuppliesId);
				if(officesupplies.getRepertory()<bo.getEntryNum()){
					throw new BusinessException("申请的数量小于库存,不能进行申请");
				}else{
					officesupplies.setRepertory(officesupplies.getRepertory()-bo.getEntryNum());
					officesupplies.store(domainProviderSet);
				}
				
			}else{
				throw new BusinessException("办公用品申请数据有误!");
			}
			bo.setState(objctForm.getState());
			bo.store(domainProviderSet);
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据id删除申请办公用品信息对象失败!", e);
		}
	}
}
